package Day02_ByteBuf_api.缓冲区;

import io.netty.buffer.*;

import java.nio.charset.StandardCharsets;

public class BufferArea {

    public static void main(String[] args) {
        // 1:堆缓冲区-
        ByteBuf heapBuffer = ByteBufAllocator.DEFAULT.heapBuffer(20);
        heapBuffer.writeBytes("cjyyp".getBytes(StandardCharsets.UTF_8));
        if (heapBuffer.hasArray()) {
            byte[] array = heapBuffer.array();
            // 计算第一个字节的偏移量
            int offset = heapBuffer.arrayOffset() + heapBuffer.readerIndex();
            // 获取可读字节数
            int len = heapBuffer.readableBytes();
        }

        // 2:堆缓冲区
        ByteBuf directBuffer = ByteBufAllocator.DEFAULT.directBuffer(20);

        // 3:复合缓冲区
        CompositeByteBuf compositeBuffer = Unpooled.compositeBuffer();//默认非池化
        compositeBuffer = ByteBufAllocator.DEFAULT.compositeBuffer(30);//默认池化
        ByteBuf headByteBuf = ByteBufAllocator.DEFAULT.heapBuffer(10);
        ByteBuf bodyByteBuf = ByteBufAllocator.DEFAULT.directBuffer(20);
        compositeBuffer.addComponents(heapBuffer,bodyByteBuf);
    }
}
